package com.aaa.controller.sds;

import cn.hutool.core.util.ObjectUtil;
import com.aaa.aspect.LogMonitor;
import com.aaa.controller.BaseController;
import com.aaa.entity.PageSortEntity;
import com.aaa.entity.Result;
import com.aaa.entity.sds.TestPlanSds;
import com.aaa.service.sds.TestPlanSdsService;
import com.github.pagehelper.Page;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * @author ：周文达
 * @date ：Created in 2022/8/30 19:17
 * @description：
 * @modified By：
 * @version:
 */
@Controller
@RequestMapping("/testPlanSds")
public class TestPlanSdsController extends BaseController {

    @Autowired
    TestPlanSdsService testPlanSdsService;

    //查询所有
    @ResponseBody
    @RequestMapping("/queryAll.do")
    public Result queryAll(PageSortEntity pageSort, TestPlanSds pp) {
        Page page = (Page) testPlanSdsService.queryAll(pageSort, pp);
        return success(page.getResult(), page.getTotal());
    }

    //添加
    @LogMonitor
    @ResponseBody
    @RequestMapping("/insert.do")
    public Result insert(TestPlanSds ts) {
        long l = testPlanSdsService.queryByTestCode();
        ts.setTestCode(l);
        boolean b = testPlanSdsService.insert(ts);
        if (b) {
            return success();
        }
        return fail();
    }

    //删除
    @LogMonitor
    @ResponseBody
    @RequestMapping("/delete.do")
    public Result delete(String id) {
        boolean b = testPlanSdsService.delete(id);
        if (b) {
            return success();
        }
        return fail();
    }

    //修改
    @LogMonitor
    @ResponseBody
    @RequestMapping("/update.do")
    public Result update(TestPlanSds ts) {
        boolean update = testPlanSdsService.update(ts);
        if (update) {
            return success();
        }
        return fail();
    }

    //名字非空校验
    @ResponseBody
    @RequestMapping("/repetitionName.do")
    public Result repetitionName(TestPlanSds testPlanSds) {
        if (ObjectUtil.isEmpty(testPlanSds.getTestName())) {
            return fail("不能为空");
        }
        boolean b = testPlanSdsService.repetitionName(testPlanSds);
        if (b) {
            return success("题目名可用");
        }
        return fail("题目重复不可用");
    }
}
